import resources.RegistNetPropertyBundle;

import registnet.dao.Grade;
import registnet.dao.GradePeer;
import registnet.dao.CoursePeer;
import registnet.dao.StudentPeer;

import resources.Constants;

GradePeer gPeer = new GradePeer();

/* [start] 	initializes gui variables */
String _change_grade = bundle.getProperty("registnet.module.grades.change_grade");

String _student_number = bundle.getProperty("registnet.module.grades.macro.change_grade.search.student_number");
String _go = bundle.getProperty("registnet.module.grades.macro.change_grade.search.go");

String _set_grade = bundle.getProperty("registnet.module.grades.macro.change_grade.actions.set_grade");

String _academic_year = bundle.getProperty("registnet.module.grades.macro.change_grade.form.academic_year");
String _semester = bundle.getProperty("registnet.module.grades.macro.change_grade.form.semester");
String _course = bundle.getProperty("registnet.module.grades.macro.change_grade.form.course");
String _section = bundle.getProperty("registnet.module.grades.macro.change_grade.form.section");
String _grade = bundle.getProperty("registnet.module.grades.macro.change_grade.form.grade");
String _remarks = bundle.getProperty("registnet.module.grades.macro.change_grade.form.remarks");
String _submit = bundle.getProperty("registnet.module.grades.macro.change_grade.form.submit");
String _cancel = bundle.getProperty("registnet.module.grades.macro.change_grade.form.cancel");

String _registrar = Constants.REGISTRAR; 
/* [end] */

/* setting the user for logs */
if(_user_type.equals(_registrar))
	gPeer.setUser(_user, _registrar);
else
	gPeer.setUser(_user, _office);
/* [end] */

/* [start]	initializes a list of grades */
List grades = new Vector();
/* [end] */



/* [start]	defines a renderer for the grades */
private class GradeRenderer implements ListitemRenderer  {
	public void render(Listitem item, java.lang.Object data) {
		registnet.dao.Student DAOStudent = data.getStudent();
		if (data.getSemester() == Constants.MAP_SEMESTER_KEY.get(Constants.SUMMER))
			new Listcell((data.getAcademicYear() - 1) + "-" + data.getAcademicYear()).setParent(item);
		else
			new Listcell(data.getAcademicYear() + "-" + (data.getAcademicYear() + 1)).setParent(item);
		new Listcell(Constants.MAP_SEMESTER_VALUE.get(data.getSemester())).setParent(item);
		new Listcell(data.getCourse().getCourseCode()).setParent(item);
		new Listcell(data.getSectionName()).setParent(item);
		new Listcell(Constants.MAP_GRADE_VALUE.get(data.getGrade())).setParent(item);
		new Listcell(data.getRemarks()).setParent(item);
	}
}
/* [end] */



/* [start] 	retrieves the grades of a student and places them on the listbox */
void retrieveGrades(String studentNumber){
	try{
		if (_user_type.equals(Constants.REGISTRAR) || (_user_type.equals(Constants.COLLEGE) && new StudentPeer().getStudent(studentNumber).getCurriculum().getDegreeCourse().getCollege().getCollegeCode().equals(_office))){
			grades = gPeer.getGradeByStudent(new StudentPeer().getStudentId(studentNumber));
			ListModel strset = new SimpleListModel(grades);
			contents_win.getFellow("grades_l").setModel(strset);
			contents_win.getFellow("grades_l").setItemRenderer(new GradeRenderer());
		}
		else
			alert("Grades cannot be modified by this account");
	}catch(Exception e){
		alert("Student not found");
	}
}
/* [end] */



/* [start]	updates the grades entry in the database;
			updates the items in the listbox;
			notifies the user if no grade is selected for modification */
void submit(){
	if(contents_win.getFellow("grades_l").getSelectedItems().size() != 0){
		try{
			String remarksValue = remarks.value;
			Double gradeValue = Constants.MAP_GRADE_KEY.get(grade.getSelectedItem().getLabel());
			Iterator iter = contents_win.getFellow("grades_l").getSelectedItems().iterator();
			while(iter.hasNext()){
				Listitem item = iter.next();
				Grade grade = grades.get(item.getIndex());
				grade.setRemarks(remarksValue);
				grade.setGrade(gradeValue);
				gPeer.updateGrade(grade);
				grades.set(item.getIndex(), grade);
			}
			ListModel strset = new SimpleListModel(grades);
			contents_win.getFellow("grades_l").setModel(strset);
			contents_win.getFellow("grades_l").setItemRenderer(new GradeRenderer());
		}catch(Exception e) { alert("Check information supplied"); }
	}
	else
		alert("No grade selected");
}
/* [end] */



/* [start]	clears the form;
			closes the form */
void cancel(){
	grade.value = null;
	remarks.value = null;
	set_grade_popup.close();
}
/* [end] */
